package com.hyx.api.controller.applet_comm;

import com.hyx.api.config.ApiPrefixRestController;
import com.hyx.api.domain.FileUpload;
import com.hyx.api.domain.User;
import com.hyx.api.domain.bo.*;
import com.hyx.api.domain.vo.MsgDialogueOrderVo;
import com.hyx.api.enums.UserTypeEnum;
import com.hyx.api.mapper.FileUploadMapper;
import com.hyx.api.service.IAppCommonService;
import com.hyx.api.service.pc_admin.*;
import com.hyx.api.utils.base.MobileBaseService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.ServerConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RequiredArgsConstructor
@ApiPrefixRestController
@RequestMapping("/appCommon")
public class AppCommonController {

    private final IMsgDialogueOrderService msgDialogueOrderService;

    @Autowired
    private IAppCommonService appCommonService;

    private final IVerifyModelOptionService verifyModelOptionService;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private FileUploadMapper fileUploadMapper;

    private final IInquiryOrderService inquiryOrderService;

    private final IRecycleOrderService recycleOrderService;

    private final MobileBaseService mobileBaseService;

    private final IFileUploadService fileUploadService;


    @GetMapping("/fileMp4")
    public AjaxResult fileMp4() {
        FileUploadBo bo = new FileUploadBo();
        bo.setType(".mp4");
        return AjaxResult.success(fileUploadService.selectFileUploadList(bo));
    }

    /**
     * 验机选项
     */
    @GetMapping("/model/options")
    public AjaxResult queryModelOptions(VerifyModelOptionBo bo) {
        bo.setStatus(1);
        return AjaxResult.success(verifyModelOptionService.selectVerifyModelOptionList(bo));
    }

    /**
     * 创建报价师账号
     *
     * @param bo
     * @return
     */
    @PostMapping("/create/offer")
    public AjaxResult createOffer(@Validated @RequestBody UserBo bo) {
        bo.setType(UserTypeEnum.OFFER_OPERATOR.code());
        return appCommonService.createUser(bo);
    }

    /**
     * 创建店员账号
     *
     * @param bo
     * @return
     */
    @PostMapping("/create/clerk")
    public AjaxResult createClerk(@Validated @RequestBody UserBo bo) {
        bo.setType(UserTypeEnum.SHOP_OPERATOR.code());
        return appCommonService.createUser(bo);
    }

    /**
     * 创建门店
     *
     * @param bo
     * @return
     */
    @PostMapping("/createShop")
    public AjaxResult createShop(@Validated @RequestBody CompanyBo bo) {
        return appCommonService.createShop(bo);
    }

    @GetMapping("/store/list")
    public AjaxResult queryStoreList() {
        return appCommonService.queryStoreAndClerkList();
    }

    @GetMapping("/offer/list")
    public AjaxResult queryOfferList() {
        return appCommonService.queryOfferList();
    }

    /**
     * 设置为店长
     */
    @PutMapping("/edit/roleStore/{id}")
    public AjaxResult editStore(@PathVariable("id") Long id) {
        System.out.println("当前id:" + id);
        return appCommonService.updateUserStore(id);
    }

    @GetMapping("/msg/list")
    public AjaxResult list(MsgDialogueOrderBo bo) {
        List<MsgDialogueOrderVo> list = msgDialogueOrderService.selectMsgDialogueOrderList(bo, mobileBaseService.getLoginUserId());
        return AjaxResult.success(list);
    }

    /**
     * 发送订单消息
     *
     * @param bo
     * @return
     */
    @PostMapping("/sendOrderMsg")
    public AjaxResult sendOrderMsg(@RequestBody MsgDialogueOrderBo bo) {
        return appCommonService.sendOrderMsg(bo);
    }

    /**
     * 读取订单消息
     *
     * @param ids
     * @return
     */
    @GetMapping("/readMsgOrder")
    public AjaxResult readMsgOrder(@RequestParam List<Long> ids) {
        return appCommonService.readMsgOrder(ids);
    }

    /**
     * 获取询价订单详情
     */
    @GetMapping("/queryInquiryOrderDetail")
    public AjaxResult queryInquiryOrderDetail(@RequestParam Long id) {
        User user = mobileBaseService.hasLogin();
        return inquiryOrderService.queryInquiryOrderDetailById(id, user);
    }

    /**
     * 获取回收订单详情
     */
    @GetMapping("/queryRecycleOrderDetail")
    public AjaxResult queryRecycleOrderDetail(@RequestParam Long id) {
        return recycleOrderService.queryRecycleOrderDetail(id);
    }

    /**
     * 获取回收订单详情_通过回收码
     */
    @GetMapping("/queryRecycleOrderDetailForCode")
    public AjaxResult queryRecycleOrderDetailForCode(@RequestParam String code) {
        return recycleOrderService.queryRecycleOrderDetailForCode(code);
    }

    /**
     * 确认支付-回收订单和追差订单
     *
     * @param orderId
     * @return
     */
    @GetMapping("/confirmPay")
    public AjaxResult confirmPay(@RequestParam Long orderId) {
        return appCommonService.confirmPay(orderId);
    }

    /**
     * 客户提现
     *
     * @param reqBo
     * @return
     */
    @PostMapping("/withdraw")
    @RepeatSubmit
    public AjaxResult withdraw(@RequestBody @Valid WithdrawReqBo reqBo) {
        return appCommonService.withdraw(reqBo);
    }

    /**
     * 更新全国行政区信息-不用对接
     *
     * @return
     */
    @GetMapping("/update/region")
    public AjaxResult updateRegionInfo() {
        return appCommonService.updateRegionInfo();
    }

    /**
     * 行政区等级
     * province：省
     * city：市
     * district：区
     *
     * @param level
     * @return
     */
    @GetMapping("/queryAreaListByType")
    public AjaxResult queryAreaListByType(@RequestParam String level) {
        return appCommonService.queryAreaListByType(level);
    }

    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file, String prefix) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            //新增文件上传信息
            FileUpload uploadDO = new FileUpload();
            uploadDO.setName(fileName);
            uploadDO.setType(fileName.substring(fileName.lastIndexOf(".")));
            uploadDO.setUrl(prefix + fileName);
            fileUploadMapper.insert(uploadDO);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileId", uploadDO.getId());
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
